package javabasics.concurrent.product_comsumer;

import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.BlockingDeque;

/**
 * @Author suxiaoxiao
 * @Date 2019/6/16 15:26
 * @Version 1.0
 */
public class Consumer implements Runnable {

    private BlockingDeque<PCData> queue;
    private static final int SLEEPTIME = 1000;

    public Consumer(BlockingDeque<PCData> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("start Consumer id = " + Thread.currentThread().getId());
        // 随机等待时间
        Random r = new Random();

        try {
            // 获取队首数据
            PCData data = queue.take();
            if (null != data) {
                int re = data.getIntData() * data.getIntData();
                System.out.println(MessageFormat
                        .format("{0} * {1} = {2}",
                                data.getIntData(), data.getIntData(), re));
                Thread.sleep(r.nextInt(SLEEPTIME));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }

    }
}
